<?php

/*
 * 
 * 后台配置健康豆
 * 
 */
namespace Admin\Controller;
use Think\Controller;
class WidgetController extends CommonController{
    
    public function index() {
        
    }
    
    /*
     * 健康豆模块
     * 1. 配置的项目列表  beans_item_list
     * 2. 添加  beans_item_add
     * 3. 修改   beans_item_modify
     * 4. 删除  beans_item_del
     * 
     * 
     */
    
    
    /*
     * 前端支付的金额配置  payment_limit_setting
     * 
     * 默认固定几个档次（比如 6 个）
     * 每个档次可以从 1元起步，整数
     * 
     */
    
    /**
     * 健康豆配置列表
     * 
     */
    public function beans_item_list(){

        if(IS_GET){

            $sys_beans_setting = M('sys_beans_setting');

            $lists = $sys_beans_setting->select();

            $this->assign('lists',$lists);
            $this->display();
        }
    }


    /**
     * 健康豆配置添加
     */
    public function beans_item_add(){

        if(IS_POST){

            $action_name = I('post.action_name');
            if(!empty($action_name)){

                $data['action_name'] = trim($action_name);
            }else{

                $this->error('action_name不能为空!','',1);
            }

            $beans = I('post.beans');
            if(!empty($beans)){

                $data['beans'] = trim($beans);
                $data['beans'] = (int)$data['beans'];
            }else{

                $this->error('健康豆不能为空!','',1);
            }

            $action  = I('post.action');
            if(!empty($action)){

                $data['action'] = trim($action);
            }else{

                $this->error('action不能为空!','',1);
            }

            $data['ctime'] = I('server.REQUEST_TIME');

            $sys_beans_setting = M('sys_beans_setting');

            $re = $sys_beans_setting->data($data)->add();

            if($re){

                $this->success('添加成功!',U('Admin/Widget/beans_item_list'),1);
            }else{

                $this->error('添加失败!','',1);
            }

        }else{

            $this->display();
        }
    }

    /**
     * 删除健康豆配置
     */
    public function beans_item_del(){

        if(IS_AJAX){

            $id = I('post.id');
            if(!empty($id)){

                $map['id'] = $id;
            }

            $sys_beans_setting = M('sys_beans_setting');

            $re = $sys_beans_setting->where($map)->delete();

            if($re){

                echo json_encode(1);
            }else{

                echo json_encode(0);
            }
        }
    }
    

    /**
     * 修改健康豆配置
     */
    public function beans_item_modify(){

        if(IS_GET){

            $id = I('get.id');
            if(!empty($id)){

                $map['id'] = $id;
            }

            $sys_beans_setting = M('sys_beans_setting');

            $list = $sys_beans_setting->where($map)->find();


            $this->assign('list',$list);
            $this->display();
        }else{

            $action_name = I('post.action_name');
            if(!empty($action_name)){

                $data['action_name'] = trim($action_name);
            }

            $beans = I('post.beans');
            if(!empty($beans)){

                $data['beans'] = trim($beans);
                $data['beans'] = (int)$data['beans'];
            }

            $action  = I('post.action');
            if(!empty($action)){

                $data['action'] = trim($action);
            }

            $id = I('post.id');
            if(!empty($id)){

                $map['id'] = $id;
            }else{

                $this->error('参数错误!','',1);
            }

            $sys_beans_setting = M('sys_beans_setting');

            $re = $sys_beans_setting->where($map)->data($data)->save();

            if($re){

                $this->success('修改成功!',U('Admin/Widget/beans_item_list'),1);
            }else{

                $this->error('修改失败!','',1);
            }
        }
    }


    /**
     * 敏感词列表
     * @return [type] [description]
     */
    public function bad_words_list(){

        if(IS_GET){

            $User = M('bad_words_filter');

            /**
            数据分页显示开始
            **/
            $count      = $User->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $show       = $Page->show();// 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $list = $User->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
            /**
            数据分页结束
            **/

            $this->assign('show',$show);
            $this->assign('list',$list);
            $this->display();
        }
    }


    /**
     * 添加敏感词
     */
    public function add_bad_words(){

        if(IS_POST){

            $DB = M('bad_words_filter');
            $data['keywords'] = trim(I('post.keywords'));
            $data['type'] = I('post.ctype');
            $data['ctime'] = I('server.REQUEST_TIME');
            // print_r($data); die;
            $re = $DB->data($data)->add();
            if(!empty($re)){
                $this->redirect('/Admin/Widget/bad_words_list');
            }
        }
    }



    /**
     * 删除敏感词
     */
    public function del_bad_words(){

        if(IS_AJAX){

            $map['id'] = I('post.id');
            $DB = M('bad_words_filter');

            $re = $DB->where($map)->delete();

            if($re){

                echo 1;
            }else{

                echo 0;
            }
        }
    }


    /**
     * 上传敏感词
     */
    public function upload_bad_words(){

        if(IS_POST){

            $upload_url = '';
            $geturl = './Uploads/'.$this->upload($_FILES['bad_words'],$upload_url);
            // echo $geturl; die;

            //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
            import("Org.Util.PHPExcel");
            //要导入的xls文件，位于根目录下的Public文件夹
            $filename=$geturl;
            //创建PHPExcel对象，注意，不能少了\
            $PHPExcel=new \PHPExcel();
            //如果excel文件后缀名为.xls，导入这个类
            import("Org.Util.PHPExcel.Reader.Excel5");
            //如果excel文件后缀名为.xlsx，导入这下类
            //import("Org.Util.PHPExcel.Reader.Excel2007");
            //$PHPReader=new \PHPExcel_Reader_Excel2007();

            $PHPReader=new \PHPExcel_Reader_Excel5();
            //载入文件
            $PHPExcel=$PHPReader->load($filename);
            //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
            $currentSheet=$PHPExcel->getSheet(0);
            //获取总列数
            $allColumn=$currentSheet->getHighestColumn();

            //获取总行数
            $allRow=$currentSheet->getHighestRow();

            //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
            for($currentRow=2;$currentRow<=$allRow;$currentRow++){
                //从哪列开始，A表示第一列
                for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){

                    //数据坐标
                    $address=$currentColumn.$currentRow;
                    //读取到的数据，保存到数组$arr中
                    $arr[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
                }
            }

            $i = 0;
            foreach ($arr as $key => $value) {

                $data[$i]['keywords'] = $value['A'];
                $data[$i]['type'] = $value['B'];
                $data[$i]['ctime'] = I('server.REQUEST_TIME');
                
                $i++;
            }

            $DB_import_excel = M('bad_words_filter');
            $res = $DB_import_excel->addAll($data);
            if(!empty($res)){

                $this->success('导入成功!',U('Admin/Widget/bad_words_list'),1);

            }else{

                $this->error('导入失败!','',1);
            }

        }
    }



    /**
     * 单文件上传
     */
    public function upload($file,$upload_url){    
        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('xls');// 设置附件上传类型    
        $upload->savePath  =     $upload_url; // 设置附件上传目录    
        // 上传单个文件     
        $info   =   $upload->uploadOne($file);    
        if(!$info) {
            // 上传错误提示错误信息        
            $this->error($upload->getError());    
            }else{
            // 上传成功 获取上传文件信息         
            return $info['savepath'].$info['savename'];    
        }
    }
    
}